package com.newdjk.newdoctor.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.newdjk.newdoctor.MyApplication;
import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.adapter.ImageAdapter;
import com.newdjk.newdoctor.adapter.MultipleItemQuickAdapter;
import com.newdjk.newdoctor.base.BasicFragment;
import com.newdjk.newdoctor.dialog.SelectedIDcardDialog;
import com.newdjk.newdoctor.entity.QuickMultipleEntity;
import com.newdjk.newdoctor.listener.TabclickListener;
import com.newdjk.newdoctor.net.BaseComPleteObserver;
import com.newdjk.newdoctor.net.NetServices;
import com.newdjk.newdoctor.ui.InviteDoctorActivity;
import com.newdjk.newdoctor.ui.MyPatientActivity;
import com.newdjk.newdoctor.ui.ShareShopActivity;
import com.newdjk.newdoctor.ui.WebViewActivity;
import com.newdjk.newdoctor.utils.Contants;
import com.newdjk.newdoctor.utils.Event;
import com.newdjk.newdoctor.utils.RxBus;
import com.newdjk.okhttp.entity.ADentity;
import com.newdjk.okhttp.entity.BaseEntity;
import com.newdjk.okhttp.entity.GetPreEntity;
import com.newdjk.okhttp.entity.HomeTabRedEntity;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class HomeFragmentcopy extends BasicFragment {


    private static final String TAG = "HomeFragment";
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.recyleview)
    RecyclerView recyleview;
    private SelectedIDcardDialog mSelectedPictureDialog;


    private List<ADentity.BannerADsBean> images = new ArrayList<>();
    private MultipleItemQuickAdapter mVideoInterrogationAdapter;
    List<QuickMultipleEntity> data = new ArrayList<>();
    private Observable<Boolean> loginObservable;
    private Observable<Boolean> mineRefeshObservable;
    private Observable<Boolean> chufangObservable;
    private Observable<Boolean> tiaopeiObservable;
    private Observable<Boolean> systemObservable;

    @Override
    protected int initViewResId() {
        return R.layout.fragment_home_copy;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initView() {
        initRecyleview();
        loginObservable = RxBus.get().register(Event.Login_success);

        loginObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                data.clear();
                initRecyleview();
                GetApplyPreListOfAuditor();
                GetApplyPreListOfChecker();
            }
        });
        mineRefeshObservable = RxBus.get().register(Event.mine_refresh);

        mineRefeshObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                data.clear();
                initRecyleview();
                GetApplyPreListOfAuditor();
                GetApplyPreListOfChecker();
            }
        });

        chufangObservable = RxBus.get().register(Event.chufang_refresh);

        chufangObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //处方审核数量
                GetApplyPreListOfAuditor();
            }
        });

        tiaopeiObservable = RxBus.get().register(Event.Tiaopei_refresh);

        tiaopeiObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //调配核对
                GetApplyPreListOfChecker();
            }
        });
        systemObservable = RxBus.get().register(Event.jpush_Sysetm_list);

        systemObservable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG, "系统消息有更新");
                //处方审核数量
                GetApplyPreListOfAuditor();
                //调配核对
                GetApplyPreListOfChecker();
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {


        loadBannerData();
        //处方审核数量
        GetApplyPreListOfAuditor();
        //调配核对
        GetApplyPreListOfChecker();
    }

    private void GetApplyPreListOfChecker() {
        List<Integer> statuslist = new ArrayList<>();
        statuslist.add(2);
        statuslist.add(5);
        GetApplyPreStatusCount(statuslist, "调配核对");
    }

    private void GetApplyPreStatusCount(List<Integer> statuslist, String type) {

        if (MyApplication.isLogin) {
            if (null != MyApplication.LoginEntity) {
                GetPreEntity getPreEntity = new GetPreEntity();
                getPreEntity.setDoctorId(MyApplication.LoginEntity.getUser().getDoctorId());
                getPreEntity.setStatus(statuslist);
                String json = new Gson().toJson(getPreEntity);
                RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
                NetServices.Factory.getService().GetApplyPreStatusCount(
                        body
                ).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseComPleteObserver<List<HomeTabRedEntity>>(getActivity(), false) {
                            @Override
                            protected void onSuccees(BaseEntity<List<HomeTabRedEntity>> t) throws Exception {
                                Log.d(TAG, "新红点 " + t.toString());
                                if (t.getData() != null) {
                                    if (t.getData().size() > 0){
                                        if (type.equals("未审方")) {
                                            int red = 0;
                                            for (int i = 0; i < t.getData().size(); i++) {
                                                red = red + t.getData().get(i).getTotalCount();
                                            }
                                            mVideoInterrogationAdapter.reFreshTab(0, red, "处方审核");
                                            //调配核对
                                        } else if (type.equals("调配核对")) {

                                            int red = 0;
                                            for (int i = 0; i < t.getData().size(); i++) {
                                                red = red + t.getData().get(i).getTotalCount();
                                            }
                                            mVideoInterrogationAdapter.reFreshTab(0, red, "调配核对");

                                        }
                                    }else {
                                        if (type.equals("未审方")) {
                                            mVideoInterrogationAdapter.reFreshTab(0, 0, "处方审核");
                                            //调配核对
                                        } else if (type.equals("调配核对")) {
                                            mVideoInterrogationAdapter.reFreshTab(0, 0, "调配核对");

                                        }
                                    }

                                }

                            }

                            @Override
                            protected void onFailure(Throwable e, String erroressage, boolean isNetWorkError) throws Exception {
                                super.onFailure(e, erroressage, isNetWorkError);

                            }
                        });

            }

        }
    }

    private void GetApplyPreListOfAuditor() {
        List<Integer> statuslist = new ArrayList<>();
        statuslist.add(0);
        GetApplyPreStatusCount(statuslist, "未审方");
    }

    private void loadBannerData() {
        initbanner();
    }

    @Override
    protected void otherViewClick(View view) {

    }

    private void initbanner() {
        ImageAdapter adapter = new ImageAdapter(images);

        banner.setAdapter(adapter)//设置适配器
//              .setCurrentItem(3,false)
                .addBannerLifecycleObserver(this)//添加生命周期观察者
//              .setBannerRound(BannerUtils.dp2px(5))//圆角
//              .addPageTransformer(new RotateYTransformer())//添加切换效果
                .setIndicator(new CircleIndicator(getContext()))//设置指示器
                .setOnBannerListener((data, position) -> {
                    LogUtils.d("position：" + position);
                });//设置点击事件,传this也行

    }

    @SuppressLint("WrongConstant")
    private void initRecyleview() {
        QuickMultipleEntity quickMultipleEntity = new QuickMultipleEntity(QuickMultipleEntity.MEDICAL);
        QuickMultipleEntity quickMultipleEntity2 = new QuickMultipleEntity(QuickMultipleEntity.OPERATE);
        QuickMultipleEntity quickMultipleEntity3 = new QuickMultipleEntity(QuickMultipleEntity.CONFIGURE);
        data.add(quickMultipleEntity);
        data.add(quickMultipleEntity2);
        data.add(quickMultipleEntity3);
        mVideoInterrogationAdapter = new MultipleItemQuickAdapter(data, getActivity(), new TabclickListener() {
            @Override
            public void onHomeFunctionClickListener(String type) {
                gotoWebview(type);
            }
        });
        //mRecyclerview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        recyleview.setAdapter(mVideoInterrogationAdapter);
        recyleview.setLayoutManager(new LinearLayoutManager(getContext(), OrientationHelper.VERTICAL, false));

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister(Event.Login_success, loginObservable);
        RxBus.get().unregister(Event.chufang_refresh, chufangObservable);
        RxBus.get().unregister(Event.Tiaopei_refresh, tiaopeiObservable);
        RxBus.get().unregister(Event.mine_refresh, mineRefeshObservable);
    }


    private void gotoWebview(String type) {

        switch (type) {
            case "药品集采":
                Intent caijiintent = new Intent(getContext(), WebViewActivity.class);
                caijiintent.putExtra(Contants.TYPE, 18);
                getContext().startActivity(caijiintent);
                break;
            case "用户管理":
                Intent paeientintent = new Intent(getContext(), MyPatientActivity.class);
                getContext().startActivity(paeientintent);
                break;
            case "快速售药":
                Intent quicksell = new Intent(getContext(), WebViewActivity.class);
                quicksell.putExtra(Contants.TYPE, 11);
                getContext().startActivity(quicksell);
                break;
            case "申请坐诊":///apply/applySitDr
                Intent applyzuozhen = new Intent(getContext(), WebViewActivity.class);
                applyzuozhen.putExtra(Contants.TYPE, 2);
                getContext().startActivity(applyzuozhen);
                break;
            case "医废申请":
                Intent yifei = new Intent(getContext(), WebViewActivity.class);
                yifei.putExtra(Contants.TYPE, 10);
                getContext().startActivity(yifei);
                break;
            case "邀请医护":

                Intent invivate = new Intent(getContext(), InviteDoctorActivity.class);
                getContext().startActivity(invivate);
                break;
            case "发布公告":
                Intent publish = new Intent(getContext(), WebViewActivity.class);
                publish.putExtra(Contants.TYPE, 8);
                getContext().startActivity(publish);
                break;
            case "申请处方":
                Intent shenqing = new Intent(getContext(), WebViewActivity.class);
                shenqing.putExtra(Contants.TYPE, 15);
                getContext().startActivity(shenqing);

                break;

            case "优选推荐":
                Intent advice = new Intent(getContext(), WebViewActivity.class);
                advice.putExtra(Contants.TYPE, 14);
                getContext().startActivity(advice);
                break;
            case "扫码入库":
                Intent scan = new Intent(getContext(), WebViewActivity.class);
                scan.putExtra(Contants.TYPE, 12);
                getContext().startActivity(scan);
                break;
            case "分享店铺":
                Intent share = new Intent(getContext(), ShareShopActivity.class);
                getContext().startActivity(share);
                break;
            case "基础设置":
                Intent basic = new Intent(getContext(), WebViewActivity.class);
                basic.putExtra(Contants.TYPE, 9);
                getContext().startActivity(basic);
                break;
            case "特色服务推荐":

                Intent sevice = new Intent(getContext(), WebViewActivity.class);
                sevice.putExtra(Contants.TYPE, 27);
                getContext().startActivity(sevice);
                break;

            case "VIP预约推荐":
                Intent vip = new Intent(getContext(), WebViewActivity.class);
                vip.putExtra(Contants.TYPE, 28);
                getContext().startActivity(vip);
                break;

            case "肺癌早筛":
                Intent threemorning = new Intent(getContext(), WebViewActivity.class);
                threemorning.putExtra(Contants.TYPE, 29);
                getContext().startActivity(threemorning);
                break;

            case "处方审核":
//                mSelectedPictureDialog = new SelectedIDcardDialog(getActivity(), this, "first");
//                mSelectedPictureDialog.show();

                Intent chufang = new Intent(getContext(), WebViewActivity.class);
                chufang.putExtra(Contants.TYPE, 34);
                getContext().startActivity(chufang);
                break;

            case "调配核对":
//                ToastUtil.setToast("调配核对");
                Intent tiaopei = new Intent(getContext(), WebViewActivity.class);
                tiaopei.putExtra(Contants.TYPE, 35);
                getContext().startActivity(tiaopei);
                break;


            case "购药回访":
                Intent huifang = new Intent(getContext(), WebViewActivity.class);
                huifang.putExtra(Contants.TYPE, 41);
                getContext().startActivity(huifang);
                break;
        }
    }


}
